package com.sz.biz.logistics.base.service.impl;

import com.sz.biz.common.constants.ErrorCodes;
import com.sz.biz.common.constants.ModuleNames;
import com.sz.biz.logistics.base.entity.CarrierCompany;
import com.sz.biz.common.base.entity.Country;
import com.sz.biz.logistics.base.service.CarrierCompanyService;
import com.sz.biz.logistics.base.dto.CarrierCompanyDto;
import com.sz.common.base.constants.UserActions;
import com.sz.common.base.dao.ParamData;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.QResultDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.service.AbstractService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.core.service.PrincipalLogUtils;
import com.sz.common.core.service.PrincipalUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Function: 承运公司CarrierCompanyServiceImpl <br>
 * Author: penghui.zheng <br>
 * Date: 2016-12-1 15:41:00
 */
@Service("carrierCompanyService")
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class CarrierCompanyServiceImpl extends AbstractService implements CarrierCompanyService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int saveCarrierCompany(CarrierCompanyDto carrierCompanyDto) {
        CarrierCompany carrierCompany = new CarrierCompany();
        EntityUtils.copyPropertiesIgnoreNull(carrierCompanyDto, carrierCompany);
        carrierCompany.setFigureCode(carrierCompany.getFigureCode().toUpperCase());
        carrierCompany.setTriadCode(carrierCompany.getTriadCode().toUpperCase());
        ParamData pd = new ParamData();
        pd.put("name", carrierCompany.getName());
        pd.put("figureCode", carrierCompany.getFigureCode());
        pd.put("triadCode", carrierCompany.getTriadCode());
        // 检查数据不能重复
        CarrierCompany figureCodeExist = (CarrierCompany) dao
                .findForObject("CarrierCompanyMapper.findByFigureCode", pd);
        if (figureCodeExist != null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_BIZ_CARRIERCOMPANY_FIGURECODE_EXIST);
        }
        dao.save("CarrierCompanyMapper.insertSelective", carrierCompany);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("ADD_CARRIER_COMPANY", carrierCompany);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_CARRIER_COMPANY, UserActions.ADD, "新建航空公司", dataBackup);
        return carrierCompany.getId();
    }

    @Override
    public int updateCarrierCompany(CarrierCompanyDto carrierCompanyDto) {
        CarrierCompany carrierCompany = new CarrierCompany();
        EntityUtils.copyPropertiesIgnoreNull(carrierCompanyDto, carrierCompany);
        carrierCompany.setFigureCode(carrierCompany.getFigureCode().toUpperCase());
        carrierCompany.setTriadCode(carrierCompany.getTriadCode().toUpperCase());
        ParamData pd = new ParamData();
        pd.put("name", carrierCompany.getName());
        pd.put("figureCode", carrierCompany.getFigureCode());
        pd.put("triadCode", carrierCompany.getTriadCode());
        pd.put("id", carrierCompany.getId());
        // 检查数据不能重复
        CarrierCompany figureCodeExist = (CarrierCompany) dao
                .findForObject("CarrierCompanyMapper.findByFigureCode", pd);
        if (figureCodeExist != null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_BIZ_CARRIERCOMPANY_FIGURECODE_EXIST);
        }
        dao.update("CarrierCompanyMapper.updateByPrimaryKeySelective", carrierCompany);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("UPDATE_CARRIER_COMPANY", carrierCompany);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_CARRIER_COMPANY, UserActions.UPDATE, "修改航空公司", dataBackup);
        return carrierCompany.getId();
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        dao.update("CarrierCompanyMapper.deleteByIds", ids);
        //保存操作日志
        Map<String, Object> dataBackup = new HashMap<>();
        dataBackup.put("DELETE_CARRIER_COMPANY", ids);
        PrincipalLogUtils.addOperationLog(ModuleNames.BIZ, ModuleNames.BIZ_CARRIER_COMPANY, UserActions.DELETE, "删除航空公司", dataBackup);
    }

    @Override
    public ResultDto findCarrierCompanyList(QueryParams params) {
        String language = PrincipalUtils.getLocalLanguage();
        params.setLanguage(language);
        ParamData pd = this.convertQueryParams(params);
        ParamData pdCountry = new ParamData();
        String query = params.getQuery();
        List<Country> allList = dao.findForList("CountryMapper" + ".findArea", pdCountry, Country.class);
        pd.put("query", query);
        // 调用方法查询数据
        List<CarrierCompanyDto> list = dao.findForList("CarrierCompanyMapper" + ".carrierCompanyListPage", pd,
                CarrierCompanyDto.class);
        for (CarrierCompanyDto carrierCompanyDto : list) {
            for (Country country : allList) {
                if (carrierCompanyDto.getCountryId().equals(country.getFigureCode())) {
                    carrierCompanyDto.setCountryName(country.getName());
                }
            }
        }
        QResultDto dto = new QResultDto();
        dto.setData(list);
        dto.setPagination(pd.getPagination());
        return dto;
    }
    @Override
    public ResultDto findShortCarrierCompanyList(QueryParams params) {
        String language = PrincipalUtils.getLocalLanguage();
        params.setLanguage(language);
        ParamData pd = this.convertQueryParams(params);
        ParamData pdCountry = new ParamData();
        String query = params.getQuery();
        List<Country> allList = dao.findForList("CountryMapper" + ".findArea", pdCountry, Country.class);
        pd.put("query", query);
        // 调用方法查询数据
        List<CarrierCompanyDto> list = dao.findForList("CarrierCompanyMapper" + ".shortCarrierCompanyListPage", pd,
                CarrierCompanyDto.class);
        for (CarrierCompanyDto carrierCompanyDto : list) {
            for (Country country : allList) {
                if (carrierCompanyDto.getCountryId().equals(country.getFigureCode())) {
                    carrierCompanyDto.setCountryName(country.getName());
                }
            }
        }
        QResultDto dto = new QResultDto();
        dto.setData(list);
        dto.setPagination(pd.getPagination());
        return dto;
    }

    @Override
    public CarrierCompanyDto findById(int id) {
        ParamData pd = new ParamData();
        List<Country> allList = dao.findForList("CountryMapper" + ".findArea", pd, Country.class);
        pd.put("id", id);
        CarrierCompanyDto carrierCompanyDto = (CarrierCompanyDto) dao.findForObject("CarrierCompanyMapper.findById", pd);
        List<I18nDto> i18n = new ArrayList<I18nDto>();
        if (carrierCompanyDto != null && carrierCompanyDto.getId() != null) {
            int refId = carrierCompanyDto.getId();
            ParamData pdi18n = new ParamData();
            pdi18n.put("refId", refId);
            i18n = dao.findForList("CarrierCompanyMapper" + ".CarrierCompanyI18n", pdi18n, I18nDto.class);
            for (Country country : allList) {
                if (carrierCompanyDto.getCountryId().equals(country.getFigureCode())) {
                    carrierCompanyDto.setCountryName(country.getName());
                }
            }
        }
        if (i18n != null) {
            carrierCompanyDto.setI18n(i18n);
        }
        return carrierCompanyDto;
    }

    /**
     * 根据id查询承运公司信息
     *
     * @param ids
     * @return CarrierCompanyDto
     */
    @Override
    public List<CarrierCompany> findByIds(List<Integer> ids) {
        Map<String,Object>map = new HashMap<>();
        map.put("language",PrincipalUtils.getLocalLanguage());
        map.put("list",ids);
         return dao.findForList("CarrierCompanyMapper.findByIds", map, CarrierCompany.class);
    }

    @SuppressWarnings("rawtypes")
    @Override
    public ResultDto findI18nByParam(QueryParams params) {
        ParamData pd = this.convertQueryParams(params);
        List entry = dao.findForList("CarrierCompanyMapper" + ".findI18nByParamListPage", pd, Map.class);
        QResultDto dto = new QResultDto(entry, pd.getPagination());
        return dto;
    }

    /**
     * 校验承运公司名称二字码
     *
     * @param figureCode
     */
    @Override
    public void checkFigureCode(String figureCode, int id) {
        Map<String, Object> map = new HashMap<>();
        map.put("figureCode", figureCode);
        if (id > 0) {
            map.put("id", id);
        }
        CarrierCompany carrierCompanyExist = (CarrierCompany) dao
                .findForObject("CarrierCompanyMapper.findByFigureCode", map);
        if (carrierCompanyExist != null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_BIZ_CARRIERCOMPANY_FIGURECODE_EXIST);
        }
    }

    /**
     * 校验承运公司三字码
     *
     * @param triadCode
     */
    @Override
    public void checkTriadCode(String triadCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("triadCode", triadCode);
        CarrierCompany carrierCompanyExist = (CarrierCompany) dao
                .findForObject("CarrierCompanyMapper.findByTriadCode", map);
        if (carrierCompanyExist != null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_BIZ_CARRIERCOMPANY_TRIADCODE_EXIST);
        }
    }

    /**
     * 承运公司名称校验
     *
     * @param name
     */
    @Override
    public void checkCarrierCompanyName(String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", name);
        CarrierCompany carrierCompanyExist = (CarrierCompany) dao
                .findForObject("CarrierCompanyMapper.findBycarrierCompanyName", map);
        if (carrierCompanyExist != null) {
            throw Exceptions.bizException(ErrorCodes.ERROR_BIZ_CARRIERCOMPANY_CARRIERCOMPANYNAME_EXIST);
        }
    }

    @Override
    protected String getMapperNamespace() {
        return "CarrierCompanyMapper";
    }
}
